package com.itheima.app.cotroller;

import com.itheima.app.manager.MovementManager;
import com.itheima.domain.mongo.Movement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.aggregation.ArrayOperators;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
public class MovementController {

    @Autowired
    private MovementManager movementManager;

    // 发布动态
    @PostMapping("/movements")
    public void saveMovement(Movement movement , MultipartFile[] imageContent) throws IOException {
        movementManager.saveMovement(movement , imageContent);
    }

    //查看我的动态
    @GetMapping("/movements/all")
    public ResponseEntity findMyMovement(
            Long userId,
            @RequestParam(value = "page",defaultValue = "1")  Integer pageNum ,
            @RequestParam(value = "pagesize",defaultValue = "10") Integer PageSize){
        return movementManager.findMyMovement(userId , pageNum , PageSize);
    }

    //查看好友动态
    @GetMapping("/movements")
    public ResponseEntity findFriendMovement(
            @RequestParam(value = "page" ,defaultValue = "1") Integer pageNum ,
            @RequestParam(value = "pagesize",defaultValue = "10") Integer pageSize){
        return movementManager.findFriendMovement(pageNum , pageSize);
    }

    //查看推荐动态
    @GetMapping("/movements/recommend")
    public ResponseEntity findRecommendMovement(
            @RequestParam(value = "page",defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize" ,defaultValue = "10") Integer pageSize){
        return movementManager.findRecommendMovement(pageNum ,pageSize);
    }


    //动态点赞
    @GetMapping("/movements/{publishId}/like")
    public ResponseEntity setMovementLike(@PathVariable String publishId){
        return movementManager.setMovementLike(publishId);
    }

    //点赞取消
    @GetMapping("/movements/{publishId}/dislike")
    public ResponseEntity removeMovementLike(@PathVariable String publishId){
        return movementManager.removeMovementLike(publishId);
    }

    //喜欢+1
    @GetMapping("/movements/{publishId}/love")
    public ResponseEntity setMovementLove(@PathVariable String publishId){
        return movementManager.setMovementLove(publishId);
    }

    //喜欢-1
    @GetMapping("/movements/{publishId}/unlove")
    public ResponseEntity removeMovementLove(@PathVariable String publishId){
        return movementManager.removeMovementLove(publishId);
    }

    //返回动态详情
    @GetMapping("/movements/{publishId}")
    public ResponseEntity findSingleMovement(@PathVariable String publishId){
        return movementManager.findSingleMovement(publishId);
    }

    //查询动态列表
    @GetMapping("/comments")
    public ResponseEntity findMovementList(@RequestParam(value = "page",defaultValue = "1") Integer pageNum ,
                                           @RequestParam(value = "pagesize" ,defaultValue = "10") Integer pageSize ,
                                           @RequestParam(value = "movementId") String publishId){
        return  movementManager.findMovementList(pageNum , pageSize ,publishId);
    }

    //发表评论
    @PostMapping("/comments")
    public void saveMovementComment(@RequestBody Map<String , String> map){
        String publishId = map.get("movementId");
        String content = map.get("comment");
        movementManager.saveMovementComment(content , publishId );
    }
}
